package org.jeecg.modules.iot.aio.task.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.utils.queue.FullWaitQueue;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 仿照tio
 * @author scw
 * @version v1.0
 * @date
 */
public abstract class AbstractQueueRunnable<T> extends BaseRunnable{

    private static final Logger log = LoggerFactory.getLogger(AbstractQueueRunnable.class);

    public AbstractQueueRunnable(ThreadPoolExecutor executor) {
        super(executor);
    }

    public boolean addMsg(T t) {
        if (this.isCanceled()) {
//            log.error("任务已经取消");
            return false;
        } else {
//            log.info("添加任务："+t.toString());
            return this.getMsgQueue().add(t);
        }
    }

    public void clearMsgQueue() {
        if (this.getMsgQueue() != null) {
            this.getMsgQueue().clear();
        }

    }

    public boolean isNeededExecute() {
        return this.getMsgQueue() != null && !this.getMsgQueue().isEmpty() && !this.isCanceled();
    }

    public abstract FullWaitQueue<T> getMsgQueue();


}
